package com.example.demo.utils

import org.apache.commons.codec.binary.Base64
import javax.crypto.Cipher
import javax.crypto.spec.IvParameterSpec
import javax.crypto.spec.SecretKeySpec

class WXBizDataCrypt internal constructor(private val sessionKey: String) {

    fun decryptData(encryptedData: String, iv: String):String {
        val aiv = Base64.decodeBase64(iv)
        val encryData = Base64.decodeBase64(encryptedData)
        val deSessionKey = Base64.decodeBase64(sessionKey)
        val ivSpec = IvParameterSpec(aiv)
        val cipher = Cipher.getInstance("AES/CBC/PKCS5Padding")
        val keySpec = SecretKeySpec(deSessionKey,"AES")
        cipher.init(Cipher.DECRYPT_MODE,keySpec,ivSpec)
        return String(cipher.doFinal(encryData),Charsets.UTF_8)
    }

}
